package itsm.isperp.module.service.bpm;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.template.Template;
import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.framework.utils.StringUtils;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.bpm.BpmRelationProcessMapDTO;
import itsm.isperp.module.entity.bpm.BpmRelationProcessMap;
import itsm.isperp.module.entity.bpm.BpmRelationProcessMapField;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.repository.bpm.BpmRelationProcessMapMapper;
import itsm.isperp.module.service.db.DbEntityService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2015-02-03 11:13
 */
@Service
public class BpmRelationProcessMapService extends
		BaseService<BpmRelationProcessMap> {

	@Autowired
	protected BpmRelationProcessMapMapper bpmRelationProcessMapMapper;

	@Autowired
	protected BpmRelationProcessMapFieldService bpmRelationProcessMapFieldService;

	@Override
	public BpmRelationProcessMapMapper getRepository() {
		return this.bpmRelationProcessMapMapper;
	}

	public BpmRelationProcessMap findByKey(String fromProcessKey,
			String toProcessKey) {
		JqGridRequest request = new JqGridRequest();
		request.addFilterRule("fromProcessKey", JqGridFilterRuleOp.eq,
				fromProcessKey);
		request.addFilterRule("toProcessKey", JqGridFilterRuleOp.eq,
				toProcessKey);

		request.isQueryOne(true);
		return this.findOneByRequest(request);

	}

	public BpmRelationProcessMapDTO[] getRelationMap(String fromId,
			String fromProcessKey, String processKey) throws SQLException,
			TemplateException {

		BpmRelationProcessMap map = findByKey(fromProcessKey, processKey);
		if (map == null) {
			return null;
		}

		List<BpmRelationProcessMapField> fieldMaps = this.bpmRelationProcessMapFieldService
				.findByRelationProcessMapId(map.getId());
		if (fieldMaps == null) {
			return null;
		}

		Map<String, Object> vars = new HashMap<String, Object>();

		DbEntityService dbEntityService = ContextHolder
				.getSpringBean("dbEntityService");

		BpmProcessSettingService bpmProcessSettingService = ContextHolder
				.getSpringBean("bpmProcessSettingService");

		DbEntity fromEntity = bpmProcessSettingService
				.findDbEntityByKey(fromProcessKey);

		Map<String, Object> entityMap = dbEntityService.getDataFromDbEntity(
				fromEntity, fromId);
		vars.put("bpmBusinessModel", entityMap);

		Template template = new Template();
		Map<String, Object> context = new HashMap<>();
		context.put("bpmBusinessModel", entityMap);
		template.setContext(context);

		BpmRelationProcessMapDTO[] dtos = new BpmRelationProcessMapDTO[fieldMaps
				.size()];
		for (int i = 0; i < dtos.length; i++) {
			BpmRelationProcessMapField fm = fieldMaps.get(i);
			template.setSource(fm.getToTemplate());
			String content = template.render();

			BpmRelationProcessMapDTO dto = new BpmRelationProcessMapDTO();
			dto.setField(StringUtils.camelCaseWord(fm.getToField()));
			dto.setValue(content);
			dtos[i] = dto;

		}

		return dtos;
	}
}
